package com.easy.service.impl;

import com.easy.annotation.LogMethod;
import com.easy.bean.Message;
import com.easy.mapper.MessageMapper;
import com.easy.service.MessageService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    MessageMapper messageMapper;

    @Override
    public Object page(Page page, Message message) {
        int count = messageMapper.count(message);
        page.setCount(count);
        List<Message> list = messageMapper.page(message, page);
        page.setData(list);
        return page;
    }

    @Override
    @LogMethod(serviceName = "新增留言信息")
    public Message add(Message message) {
        messageMapper.add(message);
        if (message.getDate() == null) {
            message.setDate(new Date()); // 自动设置为当前时间
        }
        Message result = messageMapper.one(message.getId());
        return result;
    }

    @Override
    @LogMethod(serviceName = "删除留言信息")
    public int del(Integer id) {
        return messageMapper.del(id);
    }

    @Override
    @LogMethod(serviceName = "修改留言信息")
    public Message edit(Message message) {
        messageMapper.edit(message);
        Message result = messageMapper.one(message.getId());
        return result;
    }

    @Override
    public List<Message> all(Message message) {
        return messageMapper.all(message);
    }

    @Override
    public Message one(Integer id) {
        return messageMapper.one(id);
    }
}